package com.teach.food.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.R;
import com.teach.dto.LoginUser;
import com.teach.dto.Search;
import com.teach.entity.Shops;
import com.teach.food.entity.FoodCar;
import com.teach.food.entity.FoodGoods;
import com.teach.food.entity.FoodOrderInfo;
import com.teach.food.service.FoodGoodsService;
import com.teach.mapper.FoodCarMapper;
import com.teach.mapper.FoodGoodsMapper;
import com.teach.mapper.FoodOrderInfoMapper;
import com.teach.service.ShopsService;

import lombok.extern.slf4j.Slf4j;

@Controller
@RequestMapping("/")
@Slf4j
public class FoodShangjiaController {

	@Autowired
	private FoodGoodsMapper foodgoodsmapper;

	@Autowired
	private FoodCarMapper foodmapper;

	@Autowired
	private FoodGoodsService service;

	@Autowired
	private ShopsService service1;

	@Autowired
	private FoodOrderInfoMapper orderInfoMapper;

	/**
	 * 菜单展示
	 * 
	 * @return
	 */
	@RequestMapping("shangjia.do")
	public ModelAndView shangjia(String shopId, Long id, HttpServletRequest request, Shops shop, Model model) {

		int pg1 = 1;
		try {
			pg1 = Integer.valueOf(request.getParameter("pg"));

		} catch (Exception e) {
			// TODO: handle exception
		}

		Search s = new Search();

		s.setCurrent(pg1);

		s.setRowMax(5);
		FoodGoods f = new FoodGoods();
		Long long1 = Long.valueOf(shopId);
		log.debug("{}拿到", shopId);
		f.setFoodGoodsMerchantId(long1);

		shop = service1.get(long1);
		// log.debug("1111111111{}",shop);
		String map = "gis-show.html#" + String.valueOf(shop.getPoint().getX()) + "|"
				+ String.valueOf(shop.getPoint().getY()) + "|" + shop.getShopName();
		log.debug("33333{}", map);
		// 地图
		model.addAttribute("d", shopId);
		model.addAttribute("map", map);
		// 分页数据
		PageInfo<Object> pageInfo = PageHelper.startPage(s.getCurrent(), s.getRowMax())
				.doSelectPageInfo(() -> foodgoodsmapper.selectAll(f));
		// log.debug("我的数据={}", list);
		log.debug("我的数据={}", pageInfo);
		ModelAndView mAndView = new ModelAndView();
		mAndView.addObject("p", pageInfo);
		// mAndView.addObject("all", list);

		mAndView.setViewName("/food/shangjia");

		return mAndView;
	}
	/* *//**
			 * 分页查询数据
			 *//*
				 * @RequestMapping("page.do") public ModelAndView page( Integer i) { Search
				 * s=new Search(); s.setCurrent(1); FoodGoods f=new FoodGoods(); ModelAndView
				 * mAndView = new ModelAndView();
				 * 
				 * 
				 * PageInfo<Object> pageInfo =
				 * PageHelper.startPage(s.getCurrent(),s.getRowMax()).doSelectPageInfo(()->
				 * foodgoodsmapper.selectAll(f)); mAndView.addObject("p", pageInfo);
				 * mAndView.setViewName("/food/shangjia");
				 * 
				 * return mAndView; }
				 */

	/**
	 * 购物车插入数据
	 * 
	 * @return
	 */
	@RequestMapping("gwc.do")
	@ResponseBody
	public Object gwc(String id) {
		log.debug("进入{}", id);

		FoodGoods primaryKey = foodgoodsmapper.selectByPrimaryKey(Integer.valueOf(id));
		log.debug("{}数据", primaryKey);

		FoodCar f = new FoodCar();

		f.setFoodCarId(Integer.valueOf((int) ((double) Math.random() * 50 + 1)));
		f.setFoodCarName(primaryKey.getFoodGoodsName());
		f.setFoodCarNumber(1);
		f.setFoodCarPrice(primaryKey.getFoodGoodsPrice());
		f.setFoodCarPriceCount(primaryKey.getFoodGoodsPrice());
		foodmapper.insertSelective(f);

		List<FoodCar> list = foodmapper.selectAll();

		return list;
	}

	/**
	 * 更改购物车数据刷新购物车
	 *
	 * @param list public Object pay(@RequestBody List<FoodCar> list 加入注解接收类型为json)
	 *             {
	 * @return
	 */
	@RequestMapping("pay.do")
	@ResponseBody
	public Object pay(Integer count, Float pricesum, Integer id) {

		FoodCar f = new FoodCar();
		f.setFoodCarId(id);
		f.setFoodCarNumber(count);
		f.setFoodCarPriceCount(pricesum);
		foodmapper.updateByPrimaryKeySelective(f);
		// foodmapper.delectAll();
		/*
		 * for (FoodCar foodCar : list) { FoodCar f = new FoodCar();
		 * f.setFoodCarName("人"); f.setFoodCarId(Integer.valueOf((int) ((double)
		 * Math.random() * 50 + 1))); f.setFoodCarName(foodCar.getFoodCarName());
		 * f.setFoodCarNumber(20); f.setFoodCarPrice(foodCar.getFoodCarPrice());
		 * f.setFoodCarPriceCount(foodCar.getFoodCarPrice()); foodmapper.insert(f);
		 */
		// log.debug("遍历的的集合为{}", foodCar);

		// }

		/*
		 * List<FoodCar> list1=new ArrayList<>(); list1.add(f);
		 */
		return foodmapper.selectAll();
	}

	/**
	 * 删除购物车单数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("del.do")
	@ResponseBody
	public Object del(Integer id) {

		foodmapper.deleteByPrimaryKey(id);
		;

		return R.ok();
	}

	/**
	 * 查询购物车所有数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("gwcsel.do")
	@ResponseBody
	public Object sel() {

		return foodmapper.selectAll();
	}

	/**
	 * 点击直接购买插入数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("insert.do")
	@ResponseBody
	public Object insert(String id) {
		FoodGoods primaryKey = foodgoodsmapper.selectByPrimaryKey(Integer.valueOf(id));

		FoodCar f = new FoodCar();
		f.setFoodCarId(Integer.valueOf((int) ((double) Math.random() * 50 + 1)));
		f.setFoodCarName(primaryKey.getFoodGoodsName());
		f.setFoodCarNumber(1);
		f.setFoodCarPrice(primaryKey.getFoodGoodsPrice());
		f.setFoodCarPriceCount(primaryKey.getFoodGoodsPrice());
		foodmapper.insert(f);

		return R.ok();
	}

	/**
	 * 用于跳转支付界面的展示
	 * 
	 * @return
	 */
	@RequestMapping("pay1.do")
	public ModelAndView pay1() {

		ModelAndView mAndView = new ModelAndView();

		mAndView.setViewName("/food/zhifu2");

		return mAndView;
	}

	/**
	 * 用于跳转订单修改界面
	 * 
	 * @return
	 */
	@RequestMapping("pay2.do")
	public ModelAndView pay2() {

		ModelAndView mAndView = new ModelAndView();

		mAndView.setViewName("/food/order");

		return mAndView;
	}

	/*	*//**
			 * 进入付款扫码界面
			 * 
			 * @return
			 *//*
				 * @RequestMapping("zhifu.do") public String zhifu(Model
				 * model,HttpServletRequest request) { // log.debug("{}", prc);
				 * //request.setAttribute("q", UUID.randomUUID().toString());
				 * model.addAttribute("q3", 100); model.addAttribute("q2",
				 * UUID.randomUUID().toString()); model.addAttribute("q",
				 * UUID.randomUUID().toString()); model.addAttribute("q4", "很好吃"); return
				 * "certificate.alipay.trade.page.pay"; }
				 */

	/**
	 * 更改购物车数据刷新购物车
	 *
	 * @param list public Object pay(@RequestBody List<FoodCar> list 加入注解接收类型为json)
	 *             {
	 * @return
	 */
	@RequestMapping("z.do")
	@ResponseBody
	public Object z(@RequestBody List<FoodOrderInfo> list,LoginUser loginUser) {
          log.debug("{}前台数据",list);
          log.debug("{}前台数据",loginUser.getUserName());
         for (FoodOrderInfo foodOrderInfo : list) {
        	  FoodOrderInfo f=new FoodOrderInfo();
              f.setFoodOrderId(1);
              f.setFoodOrderInfoContent(foodOrderInfo.getFoodOrderInfoContent());
              f.setFoodOrderInfoEndTime(new Date());
              f.setFoodOrderInfoId(Integer.valueOf((int) ((double) Math.random() * 100 + 1)));
              f.setFoodOrderInfoPayState(0);  
              f.setFoodOrderInfoPrice(foodOrderInfo.getFoodOrderInfoPrice());
              f.setFoodOrderInfoState(0);
              f.setFoodOrderInfoTime(new Date()); 
              f.setFoodOrderInfoUserName(loginUser.getUserName()); 
              f.setFoodOrderMerchantName("一盒糖");
              orderInfoMapper.insert(f);
		}
        
		
		return R.ok();
	}
	
	@RequestMapping("c.do") 
	public String doAlipy(Model model,HttpSession session) {
		//long id=idea.nextId();
		model.addAttribute("q", "111"); 
		model.addAttribute("q2", "222");
		//model.addAttribute("q3",25.25);
		session.setAttribute("prc", "25.25");
		model.addAttribute("q4", "1515");
		return "food/food.alipay.trade.page.pay";
	} 
	

}
